package com.itheima.d04_annotation_parse;

import org.junit.jupiter.api.Test;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Arrays;

/**
 * 测试类
 */
public class TestAnnotation {

    //解析类上的注解
    //@Test
    public void testParseClassAnnotation(){
        //1.获取Class对象
        Class<StudentService> cls = StudentService.class;

        //2.获取类上的所有注解
        Annotation[] annos = cls.getDeclaredAnnotations();
        for (Annotation anno : annos) {
            System.out.println(cls.getName()+"，注解："+ anno );
        }
        System.out.println("-----------------------");

        /*//3.判断类上有指定的注解
        if(cls.isAnnotationPresent(Operator.class)){
            //4.获取类上的指定注解
            Operator operator = cls.getAnnotation(Operator.class);
            //5.获取注解的属性值
            System.out.println("解析出注解value属性值:"+ Arrays.toString(operator.value()));
            System.out.println("解析出注解username属性值:"+ operator.username() );
        }*/

        //4.获取类中的所有方法
        Method[] methods = cls.getDeclaredMethods();
        for (Method method : methods) {
            //2.获取属性上的所有注解
            Annotation[] methodAnnos = method.getDeclaredAnnotations();
            for (Annotation anno : methodAnnos) {
                System.out.println(method.getName()+"，注解：" + anno );
            }
        }
    }

    //解析属性上的注解
    @Test
    public void testParseFieldAnnotation() throws Exception {
        //1.获取Class对象
        Class<StudentService> cls = StudentService.class;

        Field nameFiled = cls.getDeclaredField("name");
        nameFiled.setAccessible(true);

        //2.获取属性上的所有注解
        Annotation[] annos = nameFiled.getDeclaredAnnotations();
        for (Annotation anno : annos) {
            System.out.println( anno );
        }

        //3.判断属性上有指定的注解
        if(nameFiled.isAnnotationPresent(Operator.class)){
            //4.获取类上的指定注解
            Operator operator = nameFiled.getAnnotation(Operator.class);
            //5.获取注解的属性值
            System.out.println("解析出注解value属性值:"+ Arrays.toString(operator.value()));
            System.out.println("解析出注解username属性值:"+ operator.username() );
        }
    }



    //解析方法上的注解
    @Test
    public void testParseMethodAnnotation() throws Exception {
        //1.获取Class对象
        Class<StudentService> cls = StudentService.class;

        Method delMethod = cls.getDeclaredMethod("deleteStudent");

        //2.获取属性上的所有注解
        Annotation[] annos = delMethod.getDeclaredAnnotations();
        for (Annotation anno : annos) {
            System.out.println( anno );
        }

        //3.判断属性上有指定的注解
        if(delMethod.isAnnotationPresent(Operator.class)){
            //4.获取类上的指定注解
            Operator operator = delMethod.getAnnotation(Operator.class);
            //5.获取注解的属性值
            System.out.println("解析出注解value属性值:"+ Arrays.toString(operator.value()));
            System.out.println("解析出注解username属性值:"+ operator.username() );
        }
    }
}
